<?php

namespace app\api\controller;

use app\api\model\LangClassLesson;
use app\api\model\LangClassLessonFile AS LessonFileModel;
use app\api\model\OauthIntegral;
use app\api\validate\Page;

class LangClassLessonFile extends Base
{
    /**
     * 首页搜索
     * @return array
     * @throws \think\Exception\DbException
     */
    public function free()
    {
        $page = $data['page'] = input('page') ? input('page') : 1;
        $size = $data['size'] = input('size') ? input('size') : $this->pageSize;

        // 检测变量
        $validate = new Page();
        if (!$validate->check($data)) {
            return self::sendResult(102, [], $validate->getError());
        }

        $model = LessonFileModel::freeList($page, $size);
        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 获取每节课的内容，通过每节课id和课件类型（视频1，音频2，学习资料课件下载3，学习资料随堂作业4）查找
     * @return array
     * @throws \think\Exception\DbException
     */
    public function read()
    {
        // 班级id
        $classId = input('classId') ? input('classId') : 0;
        // 哪节课id
        $lessonId = input('lessonId') ? input('lessonId') : 0;
        $voice = LessonFileModel::TYPE_VIDEO;
        $type = input('type') ? input('type') : $voice;
        $page = $data['page'] = input('page') ? input('page') : 1;
        $size = $data['size'] = input('size') ? input('size') : $this->pageSize;

        if ($classId <= 0 && $lessonId <= 0) {
            return self::sendResult(100);
        }

        // 检测变量
        $validate = new Page();
        if (!$validate->check($data)) {
            return self::sendResult(102, [], $validate->getError());
        }

        $typeArr = LessonFileModel::attributes('type');
        if (!isset($typeArr[$type])) {
            return self::sendResult(102, [], '类型错误');
        }

        $oauthId = $this->oauthInfo['id'];
        $can = true;
        // 不是视频，点击班级页面其他两个tab（音频、学习资料）判断是否购买，没购买不给点击
        if ($type != $voice) {
            $can = LessonFileModel::canByLesson($oauthId, $lessonId);
        }

        if ($can === false) {
            return self::sendResult(109);
        }

        $model = LessonFileModel::getListByLesson($classId, $lessonId, $type, $page, $size);
        return self::sendResult(self::$okCode, $model);
    }

    /**
     * 通过文件id获取文件路径，判断是否已经购买，购买则返回路径
     * @param int $fileId 文件id
     * @return array
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function watch($fileId)
    {
        if ($fileId < 0) {
            return self::sendResult(100);
        }

        $oauthId = $this->oauthInfo['id'];
        // 判断文件是否可以观看
        $return = LessonFileModel::canWatch($oauthId, $fileId);
        if ($return['canWatch'] === false) {
            return self::sendResult(109);
        }
        return self::sendResult(self::$okCode, [
            'classId' => $return['classId'],
            'file' => $return['file'],
            'fileType' => $return['fileType'],
            'description' => $return['description'],
        ]);
    }
}